package com.webserver.http;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* 资源后缀与Content-Type头的对应关系
* key 资源后缀名    value Content-Type对应的值
* */
public class HttpContext {
    private static Map<String ,String >mineMapping = new HashMap<>();
    static {
        initMineMapping();//初始化mineMapping
    }
    private static void initMineMapping(){
        //initMineMapping();//初始化mimeMapping

        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read("config/web.xml");
            Element root = doc.getRootElement();
            List<Element>list = root.elements("mime-mapping");
            System.out.println(list.size());
            for(Element e : list){
                String key = e.elementText("extension");
                String value = e.elementText("mime-type");
                mineMapping.put(key,value);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        /*mineMapping.put("html","text/html");
        mineMapping.put("css","text/css");
        mineMapping.put("js","application/javascript");
        mineMapping.put("png","image/png");
        mineMapping.put("gif","image/gif");
        mineMapping.put("jpg","image/jpeg");*/
    }
    public static String getMineMapping(String ext) {
        return mineMapping.get(ext);
    }
}
